package use.thread.sync.codeblock;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SyncExample {
    public void test() {
        synchronized (this) {
            for (int i = 0; i < 100; i++) {
                System.out.print(i + " ");
            }
            System.out.println();
        }
    }

    public synchronized void test2() {
        for (int i = 0; i < 100; i++) {
            System.out.print(i + " ");
        }
        System.out.println();
    }

    public void test3() {
        synchronized (SyncExample.class) {
            for (int i = 0; i < 100; i++) {
                System.out.print(i + " ");
            }
            System.out.println();
        }
    }

    public synchronized static void test4() {
        for (int i = 0; i < 90; i++) {
            System.out.print(i + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        SyncExample syncExample1 = new SyncExample();
        SyncExample syncExample2 = new SyncExample();
        ExecutorService executorService1 = Executors.newCachedThreadPool();
        //ExecutorService executorService2 = Executors.newCachedThreadPool();
        executorService1.execute(() -> {
            syncExample1.test3();
        });
        executorService1.execute(() -> {
            syncExample1.test3();
        });
    }
}
